package com.bclz;

import java.io.IOException;

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import com.bclz.enums.WordLengthEnum;

/**
 * 
* @ClassName: WordcountMRApplication2Linux  
* @Description: 基于hadoop提供的Tool类运行Linux程序 String[] args可以接受
* 				hadoop jar xxxx.jar   WordcountMRApplication2Linux -input xxx -output xxx
*				或者hadoop jar xxxx.jar  xxx xxx
* @author xuchang
*  
* @date 2018年12月7日
 */
public class WordcountMRApplication2Linux extends Configured implements Tool {
	
	public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
		
		private final IntWritable one=new IntWritable(1);
		
		@Override
		protected void map(LongWritable key, Text value,Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			String[] split = value.toString().split(" ");
			for (String string : split) {
				if(string.length()>5) {
					context.getCounter(WordLengthEnum.MORE_THAN_FIVE).increment(1);
				}else {
					context.getCounter(WordLengthEnum.LESS_THAN_FIVE).increment(1);
				}
				context.write(new Text(string), one);
				
			}
			
		}
		
	}
	
	public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
		
		@Override
		protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			int count=0;
			for (IntWritable intWritable : values) {
				count+=intWritable.get();
			}
			context.write(key, new IntWritable(count));
		}
		
	}
	
	@Override
	public int run(String[] args) throws Exception{
		if (args == null || args.length == 0) {
			args = new String[]{
				"hdfs://ha-cluster/user/root/input",
				"hdfs://ha-cluster/user/root/output"
			};
		}
		
		
		
		Job job=Job.getInstance(getConf(),"WordCount");
		job.setJarByClass(getClass());
		
		job.setMapperClass(WordCountMapper.class);
		job.setReducerClass(WordCountReducer.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		return job.waitForCompletion(true)?0:1;
		
	}
	
	public static void main(String[] args) throws Exception {
		
//		WordcountMRApplication2Linux app=new WordcountMRApplication2Linux();
//		int result = app.run(args);
//		System.exit(result);
		int code=ToolRunner.run(new WordcountMRApplication2Linux(), args);
		System.exit(code);
	}
	
	
}
